<template>
  <h1>员工列表综合练习</h1>
  <el-form style="width: 600px;margin: 0 auto;">
    <el-form-item label="姓名">
      <el-input placeholder="请输入姓名" v-model="emp.name"></el-input>
    </el-form-item>
    <el-form-item label="工资">
      <el-input placeholder="请输入工资" v-model="emp.salary"></el-input>
    </el-form-item>
    <el-form-item label="岗位">
      <el-input placeholder="请输入岗位" v-model="emp.job"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="add()">点我提交</el-button>
    </el-form-item>
  </el-form>

  <!-- 定义表格 -->
  <el-table :data="arr" style="width:600px;margin: 0 auto;">
    <el-table-column label="编号" type="index" align="center" width="80"></el-table-column>
    <el-table-column prop="name" label="姓名" align="center"></el-table-column>
    <el-table-column prop="salary" label="工资" align="center"></el-table-column>
    <el-table-column prop="job" label="岗位" align="center"></el-table-column>
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import {ref} from "vue";
//定义数组用来保存多个员工对象
const arr = ref([]);
//定义空的员工对象,双向绑定,用来存用户输入的数据
const emp = ref({name:'',salary:'',job:''});
//定义添加员工的方法
const add = ()=>{
  //将员工信息存入数组中
  arr.value.push(emp.value);
}
const del = (i,emp)=>{
  if(confirm('是否要删除'+emp.name+'员工?')){
    //删除数组中的元素
    arr.value.splice(i,1);
  }
}
</script>

<style scoped>

</style>